import Koa from "koa";
import Router from "koa-router";
import serve from "koa-static";
import cors from "@koa/cors";
import bodyParser from "@koa/bodyparser";
import { pinyin } from "pinyin-pro";
import { getDictInstatce } from "./api/DictApi.mjs";
const app = new Koa();
const router = new Router();
router.get("/api/dict", async (ctx) => {
  ctx.body = getDictInstatce();
});
router.post("/api/pinyin", async (ctx) => {
  const chineseWords = ctx.request.body;
  if(chineseWords){
    const pinyinWords=chineseWords.msg.map((word) => {
      return pinyin(word);
    })
    ctx.body = pinyinWords
  }else{
      ctx.body = chineseWords;
  }
});
const staticDic = "./public";
app.use(bodyParser());
app.use(serve(staticDic));
app.use(router.routes());
app.use(cors());
app.listen(3000);
